import { get } from './base'

export function processSongs (songs) {
  if (!songs.length) {
    return Promise.resolve(songs)
  }
  return get('/api/getSongsUrl', {
    mid: songs.map((song) => {
      return song.mid
    })
  }).then((result) => {
    const map = result.map
    return songs
      .map((song) => {
        song.url = map[song.mid]
        return song
      })
      .filter((song) => {
        return song.url && song.url.indexOf('vkey') > -1
      })
  })
}
// 1、歌手列表、歌单列表调接口（song对象可能不一样，但对应的mid可能一样，不同的song对象可能对应同一首歌）
const lyricMap = []
export function getLyric (song) {
  if (song.lyric) {
    return Promise.resolve(song.lyric)
  }

  const mid = song.mid
  const lyric = lyricMap[mid]
  if (lyric) {
    return Promise.resolve(lyric)
  }
  // 返回promise
  return get('/api/getLyric', { mid }).then((result) => {
    // 请求成功调用then，返回歌词
    const lyric = result || '[00:00:00]该歌曲暂时无法获取歌词'
    lyricMap[mid] = lyric
    return lyric
  })
}
